package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;

public class FirstActivity extends BaseActivity {

    LinearLayout iv_splan;
    TextView first_tv;
    int i=5;
    private String password;


    private Handler sHandler = new Handler() {
        public void handleMessage(Message msg) {
            if(msg.arg1 == 0){
                first_tv.setText(msg.arg1+"秒(点击跳过)");
            }
            else{
                first_tv.setText(msg.arg1 + "秒（点击跳过）");
                startTime();
            }
        };
    };

    private void startTime() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {


            @Override
            public void run() {
                if (i > 0) {   //加入判断不能小于0
                    i--;
                    Message message = sHandler.obtainMessage();
                    message.arg1 = i;
                    sHandler.sendMessage(message);
                }
            }
        };
        timer.schedule(task, 1000);
    }
    @Override
    void initView() {
        setLayout_file(R.layout.activity_first);
        iv_splan = (LinearLayout)findViewById(R.id.iv_splan);
        first_tv=(TextView)findViewById(R.id.first_tv);
        setAlphaAnimation();
        first_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                password=getpass();
                if (password.equals("")){
                    Intent intent =  new Intent(FirstActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Intent intent1 =  new Intent(FirstActivity.this,MainActivity.class);
                    startActivity(intent1);
                    finish();
                }

            }
        });
    }

    @Override
    void initEvent() {

    }

    @Override
    void initDate() {

    }


    private void setAlphaAnimation() {
        AlphaAnimation alpha = new AlphaAnimation(0,1);
        alpha.setDuration(5000);
        alpha.setFillAfter(true);
        alpha.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                startTime();

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                password=getpass();
                if (password.equals("")){
                    Intent intent =  new Intent(FirstActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Intent intent1 =  new Intent(FirstActivity.this,MainActivity.class);
                    startActivity(intent1);
                    finish();
                }



            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        iv_splan.setAnimation(alpha);
    }

}
